from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
# import excel_info_sql
import daishu_selenium.biz_mysql
# list1 = excel_info_sql.getConditions()

def Inovel_test(Novel_name,insert_time,Chapter,Extension_name,Platform):
    driver = webdriver.Chrome(r'D:\Program Files\python3.63\chromedriver.exe')
    driver.get("https://www.ziread.cn/admin/index/login?url=%2Fadmin")
    driver.maximize_window()
    driver.implicitly_wait(10)
    time.sleep(1)

    # 定位账号
    driver.find_element_by_name('username').clear()
    driver.find_element_by_name('username').send_keys('{0}'.format(""))

    # 定位密码
 
    driver.find_element_by_name('password').clear()
    driver.find_element_by_name('password').send_keys('{0}'.format(''))

    # 点击登录
    driver.find_element_by_xpath('//div[5]/button').click()
    #点击小说推广
    try:
        time.sleep(1)
        WebDriverWait(driver, 15).until(
            EC.element_to_be_clickable((By.XPATH, '//section/ul/li[4]/a'))
        )
        driver.find_element_by_xpath('//section/ul/li[4]/a').click()
    except Exception:
        print("点击小说推广")
    #小说中心：
    try:
        # time.sleep(2)
        WebDriverWait(driver, 15).until(
            EC.element_to_be_clickable((By.XPATH, '//li[4]//li[1]//span[1]'))
        )
        driver.find_element_by_xpath('//li[4]//li[1]//span[1]').click()
    except Exception:
        print("小说中心")
    # for i in range(len(novel_name)):
    # for i in range(0, len(list1)):
    #     # 小说名字
    #     Novel_name = list1[i]["Novel_name"]
    #     # 日期
    #     insert_time = list1[i]["date_time"]
    #     # 章节
    #     Chapter = list1[i]["Chapter"]
    #     # 推广名称
    #     Extension_name = list1[i]["Extension_name"]
    #     # 平台名称
    #     Platform = list1[i]["Platform"]
    #     #点击名称
    try:
        time.sleep(1)
        driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="con_375"]/iframe'))

        WebDriverWait(driver, 15).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="name"]'))
        )

        driver.find_element_by_xpath('//*[@id="name"]').clear()
        # time.sleep(1)
        driver.find_element_by_xpath('//*[@id="name"]').send_keys(Novel_name)
    except Exception:
        print("点击名称")

    #提交
    try:
        # time.sleep(2)
        WebDriverWait(driver, 15).until(
            EC.element_to_be_clickable((By.XPATH, '//div[9]//button[1]'))
        )
        driver.find_element_by_xpath('//div[9]//button[1]').click()
    except Exception:
        print("提交")

    #推广链接
    try:
        time.sleep(1)
        WebDriverWait(driver, 15).until(
            EC.element_to_be_clickable((By.XPATH, '//td[8]/a'))
        )
        driver.find_element_by_xpath('//td[8]/a').click()
    except Exception:
        print("无推广链接")
    # print(insert_time + Extension_name)
    # time.sleep(10)

    #获取第一个推广
    try:
        time.sleep(1)
        driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="layui-layer-iframe{0}"]'.format(1)))
        driver.find_element_by_xpath('//tr[{0}]//a[3]'.format(Chapter)).click()
    except Exception:
        print("获取推广链接")

    #渠道mingcheng：
    try:
        time.sleep(1)
        driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="layui-layer-iframe1"]'))
        WebDriverWait(driver, 15).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="c-name"]'))
        )
        driver.find_element_by_xpath('//*[@id="c-name"]').clear()
        # time.sleep(1)
        driver.find_element_by_xpath('//*[@id="c-name"]').send_keys('{0}'.format(str(insert_time) + Extension_name))
    except Exception:
        print("渠道mingcheng")

    #确认
    try:
        # driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="layui-layer-iframe1"]'))
        driver.find_element_by_xpath('//button[text()="确定"]').click()
        time.sleep(1.5)
        # driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="layui-layer-iframe1"]'))
        url = driver.find_element_by_xpath('//*[@id="add-dialog"]//div[1]/div[2]/div[2]').text
        print(url)
        # 数据入库
        daishu_selenium.biz_mysql.insert_novel_mysql(url=url, Public_platform=Platform, Novel_name=Novel_name, Chapter=Chapter, datetime=str(insert_time), Extension_name=Extension_name)
    except Exception:
        print("确认出错")

    # # 退出当前iframe
    # driver.switch_to.default_content()
    #
    # #取消
    # try:
    #     time.sleep(1)
    #     driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="con_375"]/iframe'))
    #     driver.find_element_by_xpath('//*[@id="layui-layer{0}"]/span[1]/a[3]'.format(1)).click()
    # except Exception:
    #     print("取消1")
    #
    # driver.switch_to.default_content()
    # time.sleep(2)
    driver.quit()

# if __name__=="__main__":
#     Inovel_test()

